<?php

trait TConfig
{
	protected static $configLoaded = false;
	protected static $configHistory = [];
	protected static $configDefaults = [];
	public static function configGetHistory()
	{
		return implode(':', self::$configHistory);
	}
	public static function configGet($key)
	{
		self::configLoad();
		if(array_key_exists($key, self::$configData))
			return self::$configData[$key];
		//throw exception
	}
	public static function configSet($key, $value)
	{
		if(!array_key_exists($key, self::$configData))
			return; //replace with exception
		if(!array_key_exists($key, self::$configDefaults))
			self::$configDefaults[$key] = self::$configData[$key];
		self::$configData[$key] = $value;
		array_push(self::$configHistory, "[MANUAL];$key");
	}
	public static function configLoad($loadSequence = NULL, $force = false)
	{
		if($force || !self::$configLoaded)
		{
			if($loadSequence === NULL)
			{
				if(isset(self::$configFile))
					$loadSequence = self::$configFile;
				else
					$loadSequence = './test.cfg';
			}
			$files = explode(':', $loadSequence);
			foreach($files as $path)
			{
				$file = strtok($path, ';');
				$keys = NULL;
				if(strcmp($file, $path))
					$keys = explode(',', strtok(''));
				
				if(file_exists($file))
				{
					//Begin parsing
					$raw = file($file);
					foreach($raw as $line)
					{
						$line = ltrim($line);
						if(!strncmp($line, '#', 1))
							continue;
						$line = explode('=', $line, 2);
						$line[0] = rtrim($line[0]);
						$line[1] = ltrim($line[1]);
						//$line[0] - key, $line[1] - value
						if(array_key_exists($line[0], self::$configData))
						{
							if($keys === NULL || in_array($line[0], $keys))
							{
								if(!array_key_exists($line[0], self::$configDefaults))
									self::$configDefaults[$line[0]] = self::$configData[$line[0]];
								self::$configData[$line[0]] = trim($line[1], "\n\r");
							}
						}
					}
					array_push(self::$configHistory, $path);
				}
			}
			self::$configLoaded = true;
		}	
	}
	public static function configReset($key = NULL)
	{
		if($key !== NULL)
		{
			if(array_key_exists($key, self::$configDefaults))
			{
				self::$configData[$key] = self::$configDefaults[$key];
				unset(self::$configDefaults[$key]);
				array_push(self::$configHistory, '[DEFAULT];'.$key);
			}
			return;
		}
		array_merge(self::$configData, self::$configDefaults);
		self::$configDefaults = [];
		self::$configHistory = [];
	}
	public static function configExport($file, $mode = 1) 
	{
		//mode values; 1 - export differnce from defaults, 2 - export all, 3 - export defaults
		if($mode == 1)
			$array = array_uintersect_assoc(self::$configData, self::$configDefaults, create_function(NULL, "return 0;"));
		else if($mode == 2)
			$array = self::$configData;
		else if ($mode == 3)
			$array = array_merge(self::$configData, self::$configDefaults);
		else
			return; //replace with exception
		foreach($array as $key => $value)
		{
			$config .= "$key=$value\n";
		}
		file_put_contents($file, $config);
	}
	public static function configDontLoad() {
		self::$configLoaded = true;
	}
}

?>
